#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <atomic>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/time.h>
#include <ctime>
#include <boost/algorithm/string.hpp>
namespace utilNs {
    const std::string temp_path = "./temp/";

    class PathUtil {
        public :
        static std::string AddSuffix(const std::string& file_name, const std::string& Suffix) {
            return temp_path + file_name + Suffix;
        }
        static std::string Src(const std::string& file_name) {
            return AddSuffix(file_name, ".cpp");
        }

        static std::string Exe(const std::string& file_name) {
            return AddSuffix(file_name, ".exe");
        }
        static std::string Stdin(const std::string& file_name) {
            return AddSuffix(file_name, ".stdin");
        }
        static std::string Stdout(const std::string& file_name) {
            return AddSuffix(file_name, ".stdout");
        }
        static std::string Stderr(const std::string& file_name) {
            return AddSuffix(file_name, ".stderr");
        }
        static std::string ComplieErr(const std::string& file_name) {
            return AddSuffix(file_name, ".complieErr");
        }
    };


    class TimeUtil {
    public :
        // 将时间戳转化成年月日时分秒形式的字符串
        static std::string TimeStampToLocalTime(time_t currtime) {                                                                                                                                                                          
            struct tm *curr = localtime(&currtime);
            char time_buffer[128];
            snprintf(time_buffer, sizeof(time_buffer), "%d-%d-%d %d:%d:%d",\
            curr->tm_year + 1900, curr->tm_mon + 1, curr->tm_mday,\
            curr->tm_hour, curr->tm_min, curr->tm_sec);
            return time_buffer;
        }

        // 获得毫秒级时间戳
        static std::string GetTimeMs() {
            struct timeval _time;
            gettimeofday(&_time, nullptr);
            return std::to_string(_time.tv_sec * 1000 + _time.tv_usec / 1000);
        }

    };

    class FileUtil {
    public :
        // 判断一个文件是否存在
        static bool IsExistFile(const std::string& file_name) {
            struct stat st;
            int res = stat(file_name.c_str(), &st);
            return res == 0;
        }
        
        static bool ReadeFile(const std::string& file_name, std::string* content, bool keep = true) {
            (*content).clear(); // 先清理掉原有的内容
            std::ifstream in(file_name);
            if (!in.is_open()) {
                return false;
            }
            std::string line;
            while (std::getline(in, line)) {
                (*content) += line;
                if (keep) {
                    (*content) += "\n";
                }
            }
            in.close();
            return true;
        }
        static bool WriteFile(const std::string& file_name, const std::string& content) {
            std::ofstream out(file_name);
            if (!out.is_open()) {
                return false;
            }

            out.write(content.c_str(), content.size());
            out.close();
            return true;
        }

        // 获取一个唯一的文件名
        static std::string UniqueFileName() {
            static std::atomic_uint id(0);
            id++;
            std::string ms = TimeUtil::GetTimeMs();
            std::string unique_id = std::to_string(id);
            return ms + "_" + unique_id;
        }
    };

    class StringUtil {
    public :
        static void splitString(const std::string& str, std::vector<std::string>* tokens, const std::string& sep) {
            boost::split((*tokens), str, boost::is_any_of(sep), boost::algorithm::token_compress_on);
        }
    };



};